package io.renren.modules.app.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.app.annotation.Login;
import io.renren.modules.app.entity.NewsEntity;
import io.renren.modules.app.entity.NewsThumbEntity;
import io.renren.modules.app.service.NewsService;
import io.renren.modules.app.service.NewsThumbService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping({"app/news"})
public class NewsController
{

  @Autowired
  private NewsService newsService;

  @Autowired
  private NewsThumbService thumbService;

  @RequestMapping({"/list"})
  public R list(@RequestParam Map<String, Object> params)
  {
    PageUtils page = this.newsService.queryPage(params);

    return R.ok().put("page", page);
  }

  @RequestMapping({"/api/list"})
  @Login
  public R listApi(@RequestParam Map<String, Object> params)
  {
    PageUtils page = this.newsService.queryPageApp(params);
    return R.ok().put("page", page);
  }

  @RequestMapping({"/info/{newsId}"})
  public R info(@PathVariable("newsId") Integer newsId)
  {
    NewsEntity news = (NewsEntity)this.newsService.getById(newsId);

    return R.ok().put("news", news);
  }

  @RequestMapping({"/save"})
  public R save(@RequestBody NewsEntity news)
  {
    this.newsService.save(news);
    List<String> imgList = news.getImgList();
    List<NewsThumbEntity> newsThumbEntities = new ArrayList();
    for (String url : imgList) {
      NewsThumbEntity newsThumbEntity = new NewsThumbEntity();
      newsThumbEntity.setNewsId(news.getNewsId());
      newsThumbEntity.setThumbUrl(url);
      newsThumbEntities.add(newsThumbEntity);
    }
    this.thumbService.saveBatch(newsThumbEntities);
    return R.ok();
  }

  @RequestMapping({"/update"})
  public R update(@RequestBody NewsEntity news)
  {
    this.newsService.updateById(news);

    return R.ok();
  }

  @RequestMapping({"/delete"})
  @RequiresPermissions({"app:news:delete"})
  public R delete(@RequestBody Integer[] newsIds)
  {
    this.newsService.removeByIds(Arrays.asList(newsIds));

    return R.ok();
  }
}